import { Controller } from "../../mvc/core/Controller";
import { IOfflineExpController } from "./interfaces/IOfflineExpController";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { DailyRule } from "../rules/DailyRule";
import { GameController } from "../mvc/GameController";
import { MsgManager } from "../manager/MsgManager";
type int = number;
//class OfflineExpController
    
    export  class OfflineExpController extends Controller implements IOfflineExpController
    {
       

        public get time(): int
        {
            return this._time;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.onLoginGameSuccessHandler);
            Dispatcher.addEventListener(EventName.GetOfflineExp, this.onGetOfflineExpHandler);
            NetDispatcher.addCmdListener(ServerCommand.GetOfflineTimeSuccess, this.onGetTimeSuccessHandler);
            Dispatcher.addEventListener(EventName.OfflineExpPropUse, this.onOfflineExpPropUseHandler);
            return;
        }

        /* internal  */onLoginGameSuccessHandler(arg1: DataEvent): void
        {
            GameProxy.offlineExpProxy.getOfflineTime();
            return;
        }

        /* internal  */onGetOfflineExpHandler(arg1: DataEvent): void
        {
            var loc2=false;
            var loc1=arg1.data;
            if (loc1) 
            {
                if (loc1.hasOwnProperty("autoBuy")) 
                {
                    loc2 = loc1.autoBuy;
                }
                GameProxy.offlineExpProxy.gainOfflineExp(loc1.type, loc1.time, loc2);
            }
            return;
        }

        /* internal  */onGetTimeSuccessHandler(arg1: Object): void
        {
            var loc1=arg1.time as int;
            var loc2=arg1.exp as number;
            this._time = loc1;
            this._exp = loc2;
            if (loc1 > 0 || loc2 > 0) 
            {
                if (cache.role.entityInfo.level < 20 || !cache.loginNotice.isChangeVersion()) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.DailyGiftOpen, DailyRule.WelfareGet));
                }
            }
            return;
        }

        /* internal  */onOfflineExpPropUseHandler(arg1: DataEvent): void
        {
            if (this._time >= 1) 
            {
                if (GameController.loginNotice.view.isHide) 
                {
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.DailyGiftOpen, DailyRule.WelfareGet));
            }
            else 
            {
                MsgManager.showRollTipsMsg("您当前没有可以领取的离线经验");
            }
            return;
        }

        private /* var */_time: int;

        private /* var */_exp: int;
    }
